package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.allen.library.SuperTextView;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.HelpBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class HelpActivity extends MultiStatusActivity {


    @BindView(R.id.stvContainer)
    LinearLayout stvContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "帮助中心");
        loadData();
    }

    private void loadData() {
        multipleStatusView.showLoading();
        RetrofitManager.create().helpcenter_list().compose(this.<HelpBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<HelpBean>(multipleStatusView) {
                    @Override
                    public void onSuccess(HelpBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            addSuperTextView(value.getInfo());
                        }
                    }
                });

    }

    /**
     * <com.allen.library.SuperTextView
     * android:layout_width="match_parent"
     * android:layout_height="@dimen/commonBangHeight"
     * android:id="@+id/stv0"
     * app:sDividerLineType="bottom"
     * app:sLeftTextString="如何购买产品?"
     * app:sRightIconRes="@mipmap/arrow_right_red" />
     *
     * @param info
     */

    private void addSuperTextView(final List<HelpBean.InfoBean> info) {
        int size = info.size();
        for (int i = 0; i < size; i++) {
            final HelpBean.InfoBean infoBean = info.get(i);
            SuperTextView superTextView = new SuperTextView(this);
            superTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.commonBangHeight)));
            if (i != size - 1) {
                superTextView.setBottomDividerLineVisibility(View.VISIBLE);
            }
            superTextView.setLeftString(infoBean.getTitle());
            superTextView.setRightIcon(R.mipmap.arrow_right_red);
            superTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BrowerActivity.openURL(RetrofitManager.server_address + "helpcenter_info?id=" + infoBean.getId(), infoBean.getTitle(),false);
                }
            });


            stvContainer.addView(superTextView);
        }
    }

    @Override
    public int getContentView() {
        return R.layout.activity_help;
    }


}
